
class Random {
    initSeed: number
    seed: number
    seed1: number
    seed2: number
    seedType: boolean
    constructor(seed?: number) {
        this.seedType = true
        if (seed) {
            this.seed1 = Math.floor(seed / 1000000)
            this.seed2 = seed % 1000000
            this.initSeed = seed
            this.seed = this.initSeed
        } else {
            let seed1 = Math.floor(Math.random() * 233280)
            let seed2 = Math.floor(Math.random() * 233280)
            this.seed1 = seed1
            this.seed2 = seed2
            this.initSeed = seed1 * 1000000 + seed2
            this.seed = this.initSeed
        }
    }
    createSeed(): number {
        this.seed = Math.floor(Math.random() * 233280)
        let seed1 = Math.floor(Math.random() * 233280)
        let seed2 = Math.floor(Math.random() * 233280)
        this.seed1 = seed1
        this.seed2 = seed2
        return seed1 * 1000000 + seed2
    }
    setSeed(seed: number) {
        if (seed) {
            this.seed1 = Math.floor(seed / 1000000)
            this.seed2 = seed % 1000000
            this.initSeed = seed
            this.seed = this.initSeed
        }
    }
    getInitSeed() {
        return this.initSeed
    }
    getSeed() {
        return this.seed
    }
    intn(number: number): number {
        if (number == 0) {
            return 0
        }
        if (this.seedType && this.seed1) {
            let value = (9301 * this.seed1 + 49297) % 233280
            this.seed1 = value
            this.seedType = false
            return Math.ceil(value / 233280.0 * number)
        } else {
            let value = (9301 * this.seed2 + 49297) % 233280
            this.seed2 = value
            this.seedType = true
            return Math.ceil(value / 233280.0 * number)
        }


    }
    floor(number: number): number {
        if (number == 0) {
            return 0
        }
        if (this.seedType && this.seed1) {
            let value = (9301 * this.seed1 + 49297) % 233280
            this.seed1 = value
            this.seedType = false
            return value / 233280.0 * number
        } else {
            let value = (9301 * this.seed2 + 49297) % 233280
            this.seed2 = value
            this.seedType = true
            return value / 233280.0 * number
        }
    }

    getOneFromArray<T>(array: T[]): T {
        let newArray = array.slice(0)
        return newArray[Math.floor(this.floor(newArray.length))]
    }
    getRandomNumber(min: number, max: number): number {
        if (max < min) {
            return Math.floor(this.floor(min - max + 1) + max)
        } else {
            return Math.floor(this.floor(max - min + 1) + min)
        }
    }
    getRandomNumberFloor(min: number, max: number): number {
        if (max < min) {
            return this.floor(min - max + 1) + max
        } else {
            return this.floor(max - min + 1) + min
        }
    }
    getRandomArrayNoRepeat<T>(array: T[], num: number): T[] {
        let arrayCopy = array.slice(0)
        let length = arrayCopy.length
        var k = []
        if (num > length) {
            num = length
        }
        for (var i = 0; i < num; i++) {
            var l = Math.floor(this.floor(length))
            length = length - 1
            k.push(arrayCopy[l])
            arrayCopy[l] = arrayCopy[length]
        }
        return k
    }
    getRandomArrayAndRemove<T>(array: T[], num: number): T[] {
        let arrayCopy = array
        let length = arrayCopy.length
        var k = []
        if (num > length) {
            num = length
        }
        for (var i = 0; i < num; i++) {
            var l = Math.floor(this.floor(length))
            length = length - 1
            k.push(arrayCopy[l])
            arrayCopy[l] = arrayCopy[length]
            arrayCopy.pop()
        }
        return k
    }
    /** 获取数组中的一个元素并移除
     * @param array
     * @param rand
     */
    getOneFromArrayAndRemove<T>(array: T[]): T {

        let index = Math.floor(this.floor(array.length))
        let value = array[index]
        array.splice(index, 1)
        return value

    }
}
export default Random;